package com.act.moudles.user.controller;


import com.act.moudles.user.entity.Org;
import com.act.moudles.user.entity.User;
import com.act.moudles.user.service.OrgService;
import com.act.utils.DataResult;
import com.act.utils.PageSearch;
import com.act.utils.validations.AddGroup;
import com.act.utils.validations.UpdateGroup;
import com.act.utils.validations.ValidResult;
import com.act.utils.validations.ValidationUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Api(tags = "公司模块")
@RequestMapping("user/org")
@Slf4j
public class OrgController {

    @Resource
    private OrgService orgService;

    @GetMapping("listAll")
    @ApiOperation("查询所有公司名称")
    public DataResult listAll(){
        return DataResult.success("查询成功！",orgService.list(new Org().buildQuery()));
    }

    @PostMapping("pageOrg")
    @ApiOperation("分页查询公司")
    public DataResult pageOrg(@RequestBody PageSearch<Org> pageSearch){
        Page<Org> page = Page.of(pageSearch.getPageNum(),pageSearch.getPageSize());
        return DataResult.success("查询成功",orgService.page(page,pageSearch.getDomain().buildQuery()));
    }

    @PostMapping("add")
    @ApiOperation("新增部门")
    public DataResult add(@RequestBody Org org){
        // 校验接口参数
        ValidResult validResult = ValidationUtils.validateGroup(org, AddGroup.class);
        if (validResult.isHasErrors()) {
            return DataResult.fail(validResult.getErrorMsg());
        }
        //新增
        String addRes = orgService.addOrg(org);
        if(StringUtils.isNotEmpty(addRes)){
            return DataResult.fail(addRes);
        }
        return DataResult.success("新增成功");
    }

    @PostMapping("modify")
    @ApiOperation("修改部门")
    public DataResult modify(@RequestBody Org org){
        // 校验接口参数
        ValidResult validResult = ValidationUtils.validateGroup(org, UpdateGroup.class);
        if (validResult.isHasErrors()) {
            return DataResult.fail(validResult.getErrorMsg());
        }
        //修改
        String updRes = orgService.updateOrg(org);
        if(StringUtils.isNotEmpty(updRes)){
            return DataResult.fail(updRes);
        }
        return DataResult.success("修改成功");
    }



    @GetMapping("deleteFalse")
    @ApiOperation("逻辑删除部门")
    public DataResult deleteFalse(Long id){
        if(id == null){
            return DataResult.fail("主键不能未空");
        }
        Org org = orgService.getById(id);
        org.setDelFlag(0);
        orgService.updateById(org);
        return DataResult.success("删除成功");
    }

}
